/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.put.dcs_modeler.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.openide.filesystems.FileUtil;
import org.openide.util.Exceptions;
import pl.put.dcs_modeler.exceptions.DCSModelerException;

/**
 *
 * @author LukaszR
 */
public class JarUtil 
{
    private static Logger log =
            Logger.getLogger( JarUtil.class.getName() );
    
    public static List< String > getJarClassList( String path )
    {
        if ( ( path == null ) || ( path.equals( "" ) ) )
        {
            return null;
        }
        List< String > ret = new ArrayList< String >();
        {
            InputStream is = null;
            try
            {
                File f = new File( path );
                if ( !f.isFile() )
                {
                    throw new DCSModelerException( "JarUtil", "Data model configuration error. Jar doesn't exist" );
                }
                is = new FileInputStream( f );
                ZipInputStream str = new ZipInputStream( is );
                ZipEntry entry;
                while ( (entry = str.getNextEntry()) != null )
                {
                    if ( entry.isDirectory() )
                    {
                        //FileUtil.createFolder( projectRoot, entry.getName() );
                    } else
                    {
                        //log.info( "zip: " + entry.getName() );
                        String element = entry.getName();
                        
                        if ( FileUtil.getExtension( element ).equals( "class" ) )
                        {
                            ret.add( element.replace( '/', '.' ).substring( 0, element.length() - 6 ) );
                        }
                        
                        //FileUtil.getArchiveFile( "fff");
                        /*
                        FileObject fo = FileUtil.createData( projectRoot, entry.getName() );
                        if ( "nbproject/project.xml".equals( entry.getName() ) )
                        {
                        // Special handling for setting name of Ant-based projects; customize as needed:
                        filterProjectXML( fo, str, projectRoot.getName() );
                        } else
                        {
                        writeFile( str, fo );
                        }*/
                    }
                }
            } catch ( IOException ex )
            {
                Exceptions.printStackTrace( ex );
            } catch ( DCSModelerException ex )
            {
                Exceptions.printStackTrace( ex );
            }
            finally
            {
                try
                {
                    is.close();
                } catch ( IOException ex )
                {
                    Exceptions.printStackTrace( ex );
                }
            }
        }
        return ret;
    }
}
